﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Xml;

namespace Jack.Common
{
    internal class ClientDelegate<ContractType> : ClientBase<ContractType> where ContractType : class
    {
        public ClientDelegate(Binding binding, EndpointAddress endPointAddress)
            : base(binding, endPointAddress)
        { }

        public ContractType GetClientInstance()
        {
            return this.Channel;
        }
    }

    public class ClientProxy
    {
        public string Address
        {
            get;
            set;
        }

        public ContractType Create<ContractType>() where ContractType : class
        {
            return Create<ContractType>(Address);
        }

        public ContractType Create<ContractType>(string address) where ContractType : class
        {
            Binding binding = new NetTcpBinding(SecurityMode.None)
            {
                SendTimeout = new TimeSpan(0, 0, 3, 10),
                ReceiveTimeout = new TimeSpan(0, 0, 3, 10),
                OpenTimeout = new TimeSpan(0, 0, 3, 10),
                CloseTimeout = new TimeSpan(0, 0, 3, 10),
                MaxReceivedMessageSize = 120000000,
                MaxBufferPoolSize = 256,
                MaxConnections = 256,
                ListenBacklog = 256,
                TransferMode = TransferMode.Buffered,
                ReaderQuotas = new XmlDictionaryReaderQuotas { MaxArrayLength = 120000000, MaxDepth = 120000000, MaxStringContentLength = 120000000 }
            };

            return new ClientDelegate<ContractType>(binding, new EndpointAddress(address)).GetClientInstance();
        }
    }
}
